package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/4/28 0028
 * Time: 16:53
 */

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<BrandDTO> queryBrandByPage(String keyWord, Integer currentPage, String sortBy, Boolean flag, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(sortBy)) {
            String orderByClause = sortBy + (flag ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        if (StringUtils.isNoneBlank(keyWord)) {
            example.createCriteria().orLike("name", "%" + keyWord + "%").orEqualTo("letter", keyWord.toUpperCase());
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(brandList, BrandDTO.class);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);

        return new PageResult<>(brandPageInfo.getTotal(), brandDTOList);
    }

    @Override
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int insertCount = brandMapper.insertSelective(brand);
        if (insertCount != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        for (Long cid : cids) {
            insertCount = brandMapper.insertCategoryBrand(brand.getId(), cid);
            if (insertCount != 1) {
                throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
            }
        }
    }

    @Override
    public void editBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //编辑之前 删除中间表的数据通过brand_id(其实这样很不好···删除和再次添加效率上及其差)
        brandMapper.deleteCategoryCids(brand.getId());
        int updateCount = brandMapper.updateByPrimaryKeySelective(brand);
        if (updateCount != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        for (Long cid : cids) {
            updateCount = brandMapper.insertCategoryBrand(brand.getId(), cid);
            if (updateCount != 1) {
                throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
            }
        }
    }

    @Override
    public void deleteBrand(Long bid) {
        //删除中间表的数据
        brandMapper.deleteCategoryCids(bid);
        int deleteCount = brandMapper.deleteByPrimaryKey(bid);
        if (deleteCount != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public BrandDTO queryBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long id) {
        //先通过中间表查询出所有的bid
        List<Long> brandIds = brandMapper.getBrandIdsByCid(id);
        if (CollectionUtils.isEmpty(brandIds)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        List<Brand> brands = brandMapper.selectByIdList(brandIds);
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandsByIds(List<Long> ids) {
        List<Brand> list = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
